package com.zzhb;

import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipInputStream;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.zzhb.domain.Person;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ProcessVariable {

	@Autowired
	RepositoryService repositoryService;

	@Autowired
	RuntimeService runtimeService;

	@Autowired
	TaskService taskService;

	@Test
	public void testDeploy() {
		Deployment deploy = repositoryService.createDeployment().name("processVariable")
				.addClasspathResource("processes/processVariable.bpmn").deploy();
		System.out.println(deploy.getId());
		System.out.println(deploy.getName());
	}

	// 流程启动
	@Test
	public void start() {
		String key = "processVariable";
		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(key); // 默认启动最新的版本
		System.out.println(processInstance.getId());
		System.out.println(processInstance.getProcessDefinitionId());
	}

	// 设置流程参数
	@Test
	public void testSetVar() {
		Map<String, Object> variables = new HashMap<String, Object>();
		// String executionId = "";
		// runtimeService.setVariables(executionId, variables);

		String taskId = "52505";
		// taskService.setVariables(taskId, variables);
		// taskService.setVariableLocal(taskId, "local参数", "localVar");
		// taskService.setVariable(taskId, "请假天数", 3);
		// taskService.setVariable(taskId, "请假日期", new Date());
		// taskService.setVariable(taskId, "请假原因", "相亲");
		Person person = new Person();
		person.setId(10);
		person.setName("chenhj");

		taskService.setVariable(taskId, "person", person);
	}

	// 获取流程参数
	@Test
	public void testGetVar() {
		// String taskId = "47502";
		// Map<String, Object> variables = taskService.getVariables(taskId);
		// System.out.println(variables);
		String taskId = "52505";
		Person person = taskService.getVariable(taskId, "person", Person.class);
		System.out.println(person);
	}

	@Test
	public void testCompleteGetVar() {
		String taskId = "40005";
		taskService.complete(taskId);
	}

}
